﻿using Entity.Utils;
using EntityLibrary.Business.BasicInfo.Sql;
using EntityLibrary.Business.BasicInfo.Entity;
using EntityLibrary.DomainCheck;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using Dapper;

namespace EntityLibrary.Business.BasicInfo
{
    public class BasicInfoService
    {
        readonly string connStr = Decryption.StrDecrypt(ConfigurationManager.ConnectionStrings["HISDATA"].ConnectionString);

        //获取医院的科室
        public List<DepartmentInfo> GetDeptInfo(int CallNo, string CallName)
        {
            if (default(KeyValuePair<string, string>).Equals(
            CallNoType.CallNos.FirstOrDefault(c => c.Key.Equals(CallNo) && c.Value.Equals(CallName))
            ))
            {
                throw new InvalidOperationException("非法访问失败，请联系接口开发商！");
            }

            var conn = new SqlConnection(connStr);
            string sqlStr = SQLFileHelper.GetSql("GetDeptInfo.sql");
            var resultList = conn.Query<DepartmentInfo>(sqlStr).ToList();
            conn.Close();
            return resultList;
        }

        //获取医师信息
        public List<DoctInfo> GetDoctInfo(int CallNo, string CallName, string doctName = "", string deptCode = "")
        {
            if (default(KeyValuePair<string, string>).Equals(
           CallNoType.CallNos.FirstOrDefault(c => c.Key.Equals(CallNo) && c.Value.Equals(CallName))
           ))
            {
                throw new InvalidOperationException("非法访问失败，请联系接口开发商！");
            }

            var conn = new SqlConnection(connStr);
            string sqlStr = SQLFileHelper.GetSql("GetDoctInfo.sql");
            var sqlParam = new DynamicParameters();
            if (doctName.ToUpper().Equals("ALL"))
            {
                doctName = "%";
                sqlParam.Add("@YSName_In", doctName);
            }
            else
            {
                sqlParam.Add("@YSName_In","%" + doctName + "%");
            }
            if (string.IsNullOrWhiteSpace(deptCode))
            {
                deptCode = "%";
                sqlParam.Add("@KSCode_In", deptCode);
            }
            else
            {
                sqlParam.Add("@KSCode_In", "%" + deptCode + "%");
            }
            var resultList = conn.Query<DoctInfo>(sqlStr, sqlParam).ToList();
            conn.Close();
            return resultList;
        }

        //获取药品价格信息
        public List<DrugInfo> GetDrugInfo(int CallNo, string CallName, string spelCode, string itemType)
        {

            if (default(KeyValuePair<string, string>).Equals(
                CallNoType.CallNos.FirstOrDefault(c => c.Key.Equals(CallNo) && c.Value.Equals(CallName))
                ))
            {
                throw new InvalidOperationException("非法访问失败，请联系接口开发商！");
            }

            var conn = new SqlConnection(connStr);
            string sqlStr = SQLFileHelper.GetSql("GetDrugInfo.sql");
            var sqlParam = new DynamicParameters();
            sqlParam.Add("@SpelCode_In", "%" + spelCode + "%");
            sqlParam.Add("@ItemType_In", itemType);
            var resultList = conn.Query<DrugInfo>(sqlStr, sqlParam).ToList();
            conn.Close();
            return resultList;
        }

        //获取材料和诊疗项目价格信息
        public List<CheckInfo> GetCheckInfo(int CallNo, string CallName, string spelCode)
        {

            if (default(KeyValuePair<string, string>).Equals(
                CallNoType.CallNos.FirstOrDefault(c => c.Key.Equals(CallNo) && c.Value.Equals(CallName))
                ))
            {
                throw new InvalidOperationException("非法访问失败，请联系接口开发商！");
            }

            var conn = new SqlConnection(connStr);
            string sqlStr = SQLFileHelper.GetSql("GetCheckInfo.sql");
            var sqlParam = new DynamicParameters();
            sqlParam.Add("@SpelCode_In", "%" + spelCode + "%");
            var resultList = conn.Query<CheckInfo>(sqlStr, sqlParam).ToList();
            conn.Close();
            return resultList;
        }

        //获取病人住院费用明细
        public List<InHospitalCostItem> GetInHospitalCostItems(int CallNo, string CallName, string patientId, string JZDate = null)
        {
            if (default(KeyValuePair<string, string>).Equals(
                CallNoType.CallNos.FirstOrDefault(c => c.Key.Equals(CallNo) && c.Value.Equals(CallName))
                ))
            {
                throw new InvalidOperationException("非法访问失败，请联系接口开发商！");
            }

            var conn = new SqlConnection(connStr);
            string sqlStr = SQLFileHelper.GetSql("GetInHospitalCostItems.sql");
            var sqlParam = new DynamicParameters();
            sqlParam.Add("@PatientId_In", patientId);
            if (string.IsNullOrWhiteSpace(JZDate))
                JZDate = "%";
            sqlParam.Add("@JZDate_In", JZDate);
            var resultList = conn.Query<InHospitalCostItem>(sqlStr, sqlParam).ToList();
            conn.Close();
            return resultList;
        }
    }
}
